﻿Imports System.IO

Public Class CustomerRecord
    Inherits System.Windows.Forms.ListViewItem

    Public Sub New()
        MyBase.New("")
        Tag = 0
        SubItems.Add("")
    End Sub

    Public Sub New(ByVal id As Integer, ByVal first As String, ByVal last As String)
        MyBase.New(first)
        Tag = id
        SubItems.Add(last)
    End Sub

    Public Property Id() As Integer
        Get
            Return CType(MyBase.Tag, Integer)
        End Get
        Set(ByVal value As Integer)
            MyBase.Tag = value
        End Set
    End Property

    Public Property FirstName() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return MyBase.SubItems(1).Text
        End Get
        Set(ByVal value As String)
            MyBase.SubItems(1).Text = value
        End Set
    End Property

    Public ReadOnly Property SuggestName() As String
        Get
            Return FirstName & " " & LastName
        End Get
    End Property

    Public Sub Write(ByVal wtr As TextWriter)

        wtr.WriteLine("{1}{0}{2}{0}{3}", vbTab, Id, FixName(FirstName), FixName(LastName))

    End Sub

    Private Function FixName(ByVal name As String) As String
        Return name.Replace(vbTab, "").Replace(vbCr, "").Replace(vbLf, "")
    End Function

    Public Function Read(ByVal rdr As TextReader) As Boolean

        Dim arInfo As String()
        arInfo = rdr.ReadLine().Split(vbTab)
        If arInfo.Length = 3 Then
            Id = Integer.Parse(arInfo(0))
            FirstName = arInfo(1)
            LastName = arInfo(2)
            Return True
        End If

        Return False
    End Function

    Public Function Description(ByVal fileName As String) As String
        Return "Attached to " & FirstName & " " & LastName & " in document " & fileName
    End Function
End Class
